//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Coupon.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/CashFlow.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CCoupon::CCoupon (boost::shared_ptr<QuantLib::Coupon>& childNative, Object^ owner) : CCashFlow(CCoupon::typeid)
{
#ifdef HANDLE
	_phCoupon = NULL;
#endif
	_ppCoupon = &childNative;
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppCoupon));
}
Cephei::QL::Cashflows::CCoupon::CCoupon (QuantLib::Coupon& childNative, Object^ owner) : CCashFlow(CCoupon::typeid)
{
#ifdef HANDLE
	_phCoupon = NULL;
#endif
	_ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (&childNative);
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppCoupon));
    _CouponOwner = owner;
    _CashFlowOwner = owner;
}

Cephei::QL::Cashflows::CCoupon::CCoupon (CCoupon^ copy) : CCashFlow(CCoupon::typeid)
{
#ifdef HANDLE
	_phCoupon = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (copy->GetShared());
        _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppCoupon));
    }
}
Cephei::QL::Cashflows::CCoupon::CCoupon (System::Type^ t) : CCashFlow(CCoupon::typeid)
{
#ifdef HANDLE
	_phCoupon = NULL;
#endif
	if (!t->IsSubclassOf(CCoupon::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CCoupon::CCoupon (QuantLib::Handle<QuantLib::Coupon>& childNative, Object^ owner)  : CCashFlow(CCoupon::typeid)
{
	_phCoupon = &childNative;
	_ppCoupon = &static_cast<boost::shared_ptr<QuantLib::Coupon>>(childNative.currentLink());
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppCoupon));
    _CouponOwner = owner;
}
Cephei::QL::Cashflows::CCoupon::CCoupon (QuantLib::Handle<QuantLib::Coupon> childNative)  : CCashFlow(CCoupon::typeid)
{
	_phCoupon = &childNative;
	_ppCoupon = &static_cast<boost::shared_ptr<QuantLib::Coupon>>(childNative.currentLink());
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppCoupon));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CCoupon::CCoupon (QuantLib::Coupon childNative)  : CCashFlow(CCoupon::typeid)
{
#ifdef HANDLE
	_phCoupon = NULL;
#endif
	_ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (new QuantLib::Coupon (childNative));
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppCoupon));
}
#endif

Cephei::QL::Cashflows::CCoupon::~CCoupon ()
{
    if (_ppCoupon != NULL)
    {
	    delete _ppCoupon;
        _ppCoupon = NULL;
    }
}
Cephei::QL::Cashflows::CCoupon::!CCoupon ()
{
    if (_ppCoupon != NULL)
    {
	    delete _ppCoupon;
    }
}
QuantLib::Coupon& Cephei::QL::Cashflows::CCoupon::GetReference ()
{
    if (_ppCoupon == NULL) throw gcnew NativeNullException ();
	return **_ppCoupon;
}
boost::shared_ptr<QuantLib::Coupon>& Cephei::QL::Cashflows::CCoupon::GetShared ()
{
    if (_ppCoupon == NULL) throw gcnew NativeNullException ();
	return *_ppCoupon;
}
QuantLib::Coupon* Cephei::QL::Cashflows::CCoupon::GetPointer ()
{
    if (_ppCoupon == NULL) throw gcnew NativeNullException ();
	return &**_ppCoupon;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Coupon>& Cephei::QL::Cashflows::CCoupon::GetHandle ()
{
	if (_phCoupon == NULL)
	{
		_phCoupon = new Handle<QuantLib::Coupon> (*_ppCoupon);
	}
	return *_phCoupon;
}
#endif
bool Cephei::QL::Cashflows::CCoupon::HasNative () 
{
	return (_ppCoupon != NULL);
}

Int64 Cephei::QL::Cashflows::CCoupon::AccrualDays::get ()
{
    try
    {
    	QuantLib::BigInteger _rv = (QuantLib::BigInteger)(*_ppCoupon)->accrualDays ( );   
        Int64 _nrv = (Int64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Cashflows::CCoupon::AccrualEndDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCoupon)->accrualEndDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCoupon::AccrualPeriod::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppCoupon)->accrualPeriod ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Cashflows::CCoupon::AccrualStartDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCoupon)->accrualStartDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCoupon::AccruedAmount (DateTime prm1)
{
    try
    {
        QuantLib::Date _prm1 = (QuantLib::Date)ValueHelper::Convert (prm1);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCoupon)->accruedAmount ( _prm1 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Cashflows::CCoupon::Date::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCoupon)->date ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Cashflows::CCoupon::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppCoupon)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCoupon::Nominal::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCoupon)->nominal ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCoupon::Rate::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCoupon)->rate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Cashflows::CCoupon::ReferencePeriodEnd::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCoupon)->referencePeriodEnd ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Cashflows::CCoupon::ReferencePeriodStart::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCoupon)->referencePeriodStart ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

